package com.lanou.zy.projecta.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.util.OnItemClickListener;
import com.lanou.zy.projecta.volley.VolleySingleton;
import com.lanou.zy.projecta.entity.Community;

import java.util.List;

/**
 * Created by dllo on 15/11/2.
 */
public class CommunityGridViewAdapter extends BaseAdapter {
    private List<Community.DataEntity.ForumListEntity.GroupEntity> cdfg;
    private ImageLoader imageLoader;
    private LayoutInflater inflater;

    public CommunityGridViewAdapter(Context context, List<Community.DataEntity.ForumListEntity.GroupEntity> datas) {
        cdfg = datas;
        inflater = LayoutInflater.from(context);
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    @Override
    public int getCount() {
        return cdfg != null && cdfg.size() > 0 ? cdfg.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return cdfg.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View v, ViewGroup viewGroup) {
        CommunityViewHolder holder = null;
        if (v == null) {
            v = inflater.inflate(R.layout.community_gridview_item, null);
            holder = new CommunityViewHolder(v);
            v.setTag(holder);

        } else {
            holder = (CommunityViewHolder) v.getTag();
        }
        Community.DataEntity.ForumListEntity.GroupEntity c = cdfg.get(position);
        holder.communityTitleTv.setText(c.getName());
        holder.communityContentTv.setText(c.getTotal_threads() + "个帖子");
        holder.communityIv.setImageUrl(c.getPhoto(), imageLoader);

        return v;
    }


    public class CommunityViewHolder {
        NetworkImageView communityIv;
        TextView communityTitleTv, communityContentTv;

        public CommunityViewHolder(View v) {
            communityIv = (NetworkImageView) v.findViewById(R.id.communityIv);
            communityTitleTv = (TextView) v.findViewById(R.id.communityTitleTv);
            communityContentTv = (TextView) v.findViewById(R.id.communityContentTv);
        }
    }
}
